package com.guanlixitong.service.impl;

import com.guanlixitong.dao.IStudentDao;
import com.guanlixitong.domain.Student;
import com.guanlixitong.factory.Factory;
import com.guanlixitong.service.IStudentService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;

public class StudentServiceImpl implements IStudentService {

    private IStudentDao studentDao= Factory.getStudentDao();

    private Scanner sc = new Scanner(System.in);

    //判断学号是否存在
    @Override
    public boolean isExists(String sid) throws Exception {
        ArrayList<Student> all = studentDao.findAllstudent();
        for (int i = 0; i < all.size(); i++) {
            if(sid.equals(all.get(i).getId())){
                return true;
            }
        }
        return false;
    }

    //增
    @Override
    public void addstudent(Student student) throws IOException {
        studentDao.addstudent(student);
    }

    //删
    @Override
    public void deletestudent(String sid) throws IOException {
        studentDao.deletestudent(sid);
    }

    //改
    @Override
    public void updatestudent(String sid, Student student) throws IOException {
        studentDao.updatestudent(sid, student);
    }

    //查
    @Override
    public ArrayList<Student> findAllstudent() throws IOException {
        return studentDao.findAllstudent();
    }

    //更新数据
    @Override
    public void tst() throws IOException {
         studentDao.tst();
    }

    //导出
    @Override
    public void excel() throws Exception {
        studentDao.excel();
    }

    //导入
    @Override
    public void importExcel(String s) throws Exception {
        studentDao.importExcel(s);
    }

    //读日志
    @Override
    public void readRizhi() throws Exception {
        studentDao.readRizhi();
    }

    //统计
    @Override
    public void Tongji(String choice2)  {
        try {
            ArrayList<Student> list = studentDao.findAllstudent();
            switch (choice2){
                case "1":
                    findMW(list);
                    break;
                case "2":
                    findWx2(list);
                    break;
                case "3":
                    find00(list);
                    break;
                case "4":
                    find62(list);
                    break;
                case "5":
                    findXing(list);
                    break;
                case "6":
                    findName3(list);
                    break;
                case "7":
                    findNumber(list);
                    break;
                case "8":
                    findAgeDuan(list);
                    break;
                case "9":
                    findAge(list);
                    break;
                case "10":
                    findAveAge(list);
                    break;
                case "11":
                    findGroup(list);
                    break;
                case "12":
                    findXings(list);
                    break;
                case "13":
                    findSubjects(list);
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //统计科目
    private void findSubjects(ArrayList<Student> list) {
        HashMap<String, Integer> subjectGe = findSubjectGe(list);

        Set<String> subjectSet = subjectGe.keySet();
        subjectSet.stream().forEach(k->{
            System.out.println(k + "科目的人数是：" + subjectGe.get(k) + "个");
        });
    }

    //统计科目人数
    private HashMap<String, Integer> findSubjectGe(ArrayList<Student> list) {

            HashMap<String, ArrayList<Student>> map = new HashMap<>();
            for (Student student : list) {
                String s= student.getSubject();
                if (map.containsKey(s)){
                    map.get(s).add(student);
                }else{
                    ArrayList<Student> students = new ArrayList<>();
                    students.add(student);
                    map.put(s,students);
                }
            }
            HashMap<String, Integer> subjects = new HashMap<>();
            Set<String> keySet = map.keySet();
            for (String s: keySet) {
                ArrayList<Student> students = map.get(s);

                subjects.put(s,students.size());
            }
            return subjects;
    }

    //退出
    @Override
    public void exit() throws IOException {
        studentDao.exit();
    }

    //各组人数
    private void findGroup(ArrayList<Student> list) {
        HashMap<Integer, Integer> groupGe = findGroupGe(list);

        Set<Integer> groupSet = groupGe.keySet();
        groupSet.stream().forEach(k->{
            System.out.println(k + "组的人数是：" + groupGe.get(k) + "个");
        });
    }

    //统计各组人数
    public  HashMap<Integer,Integer> findGroupGe(ArrayList<Student> list){

        HashMap<Integer, ArrayList<Student>> map = new HashMap<>();
        for (Student student : list) {
            int group = student.getGroup();
            if (map.containsKey(group)){
                map.get(group).add(student);
            }else{
                ArrayList<Student> students = new ArrayList<>();
                students.add(student);
                map.put(group,students);
            }
        }
        HashMap<Integer, Integer> groups = new HashMap<>();
        Set<Integer> keySet = map.keySet();
        for (Integer s: keySet) {
            ArrayList<Student> students = map.get(s);

            groups.put(s,students.size());
        }
        return groups;
    }

    //各个姓
    private void findXings(ArrayList<Student> list) {
        HashMap<String, Integer> xingGe = findXingGe(list);

        Set<String> xingSet = xingGe.keySet();
        xingSet.stream().forEach(k->{
            System.out.println(k + "姓的人数是：" + xingGe.get(k) + "个");
        });
    }

    //平均年龄
    private void findAveAge(ArrayList<Student> list) {

        HashMap<Integer, Integer> aveAge = getAveAge(list);
        aveAge.forEach((k,v)->{
            System.out.println(k + "组的年龄是：" + v + "岁");
        });
    }

    //统计平均年龄
    public  HashMap<Integer,Integer> getAveAge(ArrayList<Student> list){
        HashMap<Integer, ArrayList<Student>> map = new HashMap<>();
        for (Student student : list) {
            int group = student.getGroup();
            if (map.containsKey(group)){
                map.get(group).add(student);
            }else{
                ArrayList<Student> students = new ArrayList<>();
                students.add(student);
                map.put(group,students);
            }
        }

        HashMap<Integer, Integer> aveAge = new HashMap<>();
        Set<Integer> keySet = map.keySet();
        for (Integer group : keySet) {
            ArrayList<Student> students = map.get(group);

            int sum = 0;
            for (int i = 0; i < students.size(); i++) {
                Student student = students.get(i);
                int age = student.getAge();
                sum += age;
            }
            int avg = sum/students.size();

            aveAge.put(group,avg);
        }
        return aveAge;
    }

    //年龄全体
    public  void findAge(ArrayList<Student> list) {

        HashMap<Integer, Integer> ageDuan = getAgeDuan(list);
        ageDuan.forEach((k,v)->{
            System.out.println(k + "岁年龄段的人数是：" + v + "个");
        });
    }

    //年龄段
    public void findAgeDuan(ArrayList<Student> list) {

        HashMap<Integer, Integer> ageDuan = getAgeDuan(list);

        for (int i = 18; i <= 20; i++) {
            System.out.println((i) + "岁年龄段的人数是：" + ageDuan.get(i) + "个");
        }

    }

    //班级人数
    public void findNumber(ArrayList<Student> list) {

        int size = list.size();
        System.out.println("班里有"+size+"人");
    }

    //00后
    public void find00(ArrayList<Student> list){

        long count = list.stream().filter(s -> s.getBirthday().getYear() >= 2000).count();
        System.out.println("00后有"+count + "人");
    }

    //手机号中至少包含2个6的
    public  void find62(ArrayList<Student> list) {

        long count = list.stream().filter(s -> s.getPhone().replace("6", "").length() <= 9).count();
        System.out.println("手机号中至少包含2个6的有"+count+"人");
    }

    //姓
    public  void findXing(ArrayList<Student> list){

        System.out.println("请输入要查找的姓：");
        String xing = sc.nextLine();
        HashMap<String, Integer> map = findXingGe(list);
        Integer integer = map.get(xing);
        System.out.println("姓"+xing+"的有"+integer+"人");

    }

    //名字长度为3的学生有
    public  void findName3(ArrayList<Student> list){

        long count = list.stream().filter(s -> s.getName().length() == 3).count();
        System.out.println("名字长度为3的学生有"+count+"人");
    }

    //男女
    public  void findMW(ArrayList<Student> list){

        long man = list.stream().filter(s -> s.getSex().equals("男")).count();
        System.out.println("男生有"+man+"人");
        long woman = list.stream().filter(s -> s.getSex().equals("女")).count();
        System.out.println("女生有"+woman+"人");

    }

    //有2个的微信号的人个数
    public  void findWx2(ArrayList<Student> list){

        long count = list.stream().filter(s -> s.getList().toString().split(",").length == 2).count();
        System.out.println("有2个的微信号的人有"+count+"个");
    }

    //统计各个姓的人数
    public  HashMap<String,Integer> findXingGe(ArrayList<Student> list){

        HashMap<String, ArrayList<Student>> map = new HashMap<>();
        for (Student student : list) {
            String s= student.getName().substring(0, 1);
            if (map.containsKey(s)){
                map.get(s).add(student);
            }else{
                ArrayList<Student> students = new ArrayList<>();
                students.add(student);
                map.put(s,students);
            }
        }
        HashMap<String, Integer> Xings = new HashMap<>();
        Set<String> keySet = map.keySet();
        for (String s: keySet) {
            ArrayList<Student> students = map.get(s);

            Xings.put(s,students.size());
        }
        return Xings;
    }

    //统计年龄段人数
    public static HashMap<Integer,Integer> getAgeDuan(ArrayList<Student> list){

        HashMap<Integer, ArrayList<Student>> map = new HashMap<>();
        for (Student student : list) {
            int age = student.getAge();
            if (map.containsKey(age)){
                map.get(age).add(student);
            }else{
                ArrayList<Student> students = new ArrayList<>();
                students.add(student);
                map.put(age,students);
            }
        }
        HashMap<Integer, Integer> AgeD = new HashMap<>();
        Set<Integer> keySet = map.keySet();
        for (Integer age : keySet) {
            ArrayList<Student> students = map.get(age);

            AgeD.put(age,students.size());
        }
        return AgeD;
    }
}
